// pages/staff/index/index.ts


import {
  orderListSearch,
  leaderOrderList,
  staffOrderList

} from "../../../http/api";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    date: '',
    show: false,
    orderArr: [],
    orderType: "",
    minDate: new Date(2024, 0, 1).getTime(),
    maxDate: new Date(2028, 0, 31).getTime(),
    amountAllOrderAmount: 0,
    amountDayOrder: 0,
    amountMonthOrder: 0,
    isLoadingMore: false,
    pageNum: 1,
    pageSize: 10,
    noMore: false,
  },

  handleChangeOrderType(e) {
    const {
      type
    } = e.currentTarget.dataset
    this.setData({
      orderType: type,
      pageNum: 1,
      noMore: false
    })
    this.getData()

  },

  handlePhone(e) {

    const {
      phone
    } = e.currentTarget.dataset

    wx.makePhoneCall({
      phoneNumber: phone //仅为示例，并非真实的电话号码
    })

  },


  onClose() {
    this.setData({
      show: false
    });
  },
  onDisplay() {

    this.setData({
      show: true
    });
  },
  formatDate(date) {
    date = new Date(date);
    return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`;
  },
  onConfirm(event) {
    const [start, end] = event.detail;
    this.setData({
      show: false,
      date: `${this.formatDate(start)} 至 ${this.formatDate(end)}`,
      pageNum: 1,
      noMore: false
    });
    this.getData()
  },
  toPage(e) {
    const {
      url
    } = e.currentTarget.dataset


    wx.navigateTo({
      url
    })
  },



  getData() {

    const {
      date,
      pageNum,
      pageSize,
      noMore
    } = this.data

    if (noMore) {
      wx.showToast({
        title: '没有更多订单',
        icon: 'none'
      })
      return
    }




    let dateArr;
    let params = {
      startOrderDate: "",
      endOrderDate: "",
      orderType: this.data.orderType,
      staffId: wx.getStorageSync('staffId'),
      pageNum,
      pageSize
    }
    if (date) {
      dateArr = date.split("至")
      params.startOrderDate = dateArr[0];
      params.endOrderDate = dateArr[1]
    }



    wx.showLoading()
    staffOrderList(params).then(res => {
      if (res.data.code === 200) {
        if (res.data.dataList.length == 0 && this.data.pageNum>1) {
          wx.showToast({
            title: '没有更多订单',
            icon: 'none'
          })
          this.setData({
            noMore: true
          })
        } else {
          // this.setData({
          //   orderArr: [this.data.orderArr, ...res.data.dataList]
          // })
          const {
            amountAllOrderAmount,
            amountDayOrder,
            amountMonthOrder
          } = res.data

          this.setData({
            amountAllOrderAmount,
            amountDayOrder,
            amountMonthOrder
          })
          if (this.data.pageNum > 1) {

            this.setData({
              orderArr: [...this.data.orderArr, ...res.data.dataList]
            })

          } else {
            this.setData({
              orderArr: [...res.data.dataList]
            })
          }

        }


      }
    }).finally(() => {
      this.setData({
        isLoadingMore: false
      })
      wx.hideLoading()

    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getData()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (!this.data.isLoadingMore && !this.data.noMore) {
      const pageNum = this.data.pageNum + 1;
      this.setData({
        pageNum
      })
      this.getData()

    }

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})